package edu.wfu.preparedDishes.user.controller;

import java.util.Arrays;
import java.util.Map;

import edu.wfu.preparedDishes.common.utils.PageUtils;
import edu.wfu.preparedDishes.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import edu.wfu.preparedDishes.user.entity.UserEntity;
import edu.wfu.preparedDishes.user.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 用户表;
 *
 * @author hero_miracle
 * @email hero_miracle@gmail.com
 * @date 2023-11-18 21:48:21
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("user/user")
public class UserController {
    @Resource
    private UserService userService;
    // 注入request
    @Resource
    private HttpServletRequest request;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    public R info(HttpServletRequest request){
        String userId = (String)request.getAttribute("userId");
		UserEntity user = userService.getById(userId);
        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete" )
    public R delete(@RequestBody String[] userIds){
		userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }
    /**
     * 登录
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public R login (@RequestBody Map<String, String> params){
        return R.ok(userService.addUser(params.get("code")));
    }


}
